package com.mj.action.system;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONArray;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mj.base.BaseController;
import com.mj.model.system.Dept;
import com.mj.service.system.DeptService;
import com.mj.util.CommonUtil;

/**
 * 部门组织刮泥
 *@ClassName:DeptAction.java
 *@author dsc
 *@date 2017年7月25日
 *@Description TODO
 *
 */
@Controller
@RequestMapping("dept")
public class DeptAction extends BaseController{
	@Resource
	private DeptService deptService;
	
    @Resource
    CommonUtil commonUtil;
	
    
	
	/**
	 * 列表，grid形式
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="deptGrid")
	public String deptGrid(){
		//部门名称
		String deptName = request.getParameter("deptName");
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("deptName", deptName);
    	//部门列表信息
    	List<Dept> deptlist = deptService.findAllDept(map);
    	//是否根据menuname返回list，处理
    	List<Map<String,Object>> mlist = deptService.loadByDeptName(deptlist, deptName);
		JSONArray objarr = JSONArray.fromObject(mlist);
        return renderJsonGrid(objarr, mlist.size());
	}
	
	//部门列表页面
	@RequestMapping(value = "")
	public String toDeptList(Model model){
		return "system/dept/dept_list"; 
	}
	
	
	/**
	 * 部门菜单
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "getDeptComTree")
	public String getDeptComTree(Model model){
		List<Dept> menulist = deptService.findAllDept(null);
		List<Map<String,Object>> ml = commonUtil.deptList(menulist, 0);
		return renderJsonArray(ml); 
	}
	
	
	/**
	 * 添加部门
	 * @param menu
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="addDept")
	public Boolean addDept(Dept dept){
		int flag = deptService.insertSelective(dept);
		if(flag > 0){
			return true;
		}else{
			return false;
		}
	}
//	/**
//	 * 软删除菜单
//	 *  
//	 * @return
//	 */
//	@ResponseBody
//	@RequestMapping(value="/delDept")
//	public Boolean delDept(){
//		int deptId =Integer.parseInt(request.getParameter("dept_id"));
//		int available =Integer.parseInt(request.getParameter("available"));
//		int flag = deptService.updateByDeptId(deptId,available);
//		if(flag > 0){
//			return true;
//		}else{
//			return false;
//		}
//	}
	
	/**
	 * 编辑部门信息
	 * @param menu
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="editDept")
	public Boolean editDept(Dept dept){
		int flag = deptService.updateByPrimaryKeySelective(dept);
		if(flag > 0){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 查询菜单
	 */
	@ResponseBody
	@RequestMapping(value="getDeptById")
	public String getDeptById(){
		Dept u = deptService.selectByPrimaryKey(Integer.valueOf(request.getParameter("uid")));
		return renderJson(u);
	}
	
}
